﻿module lang.d.sts.dmodule;

import lang.d.sts.dsyntaxelement;
import lang.d.sts.iaggregate;
import lang.d.sts.declarations.ddeclaration;
import lang.d.sts.declarations.dimport;
import lang.d.visitors.dvisitor;
import std.string;

class DModule: DSyntaxElement, IAggregate {
    private string myModuleName;
    private string mySystem;
    private DImport[][string] myImportedModules;
    private DDeclaration[] myDeclarations;

    public this(string moduleName = "") {
        myModuleName = moduleName;
    }

    public void setModuleName(string name) {
        myModuleName = name;
    }

    public void setSystem(in string system) {
        mySystem = system;
    }

    public string getModuleName() const {
        return myModuleName;
    }

    public string getSystem() const {
        return mySystem;
    }

    public string getPackageName() const {
        if (myModuleName == "") {
            return "";
        }
        return myModuleName[0..lastIndexOf(myModuleName, ".")];
    }

    override public void addDeclaration(DDeclaration declaration) {
        myDeclarations ~= declaration;
        declaration.setParent(this);
    }

    override public void addImport(DImport imp) {
        string prefix = imp.getPrefix();
        if (prefix in myImportedModules) {
            myImportedModules[prefix] ~= imp;
        } else {
            myImportedModules[prefix] = [imp];
        }
        imp.setParent(this);
    }

    public DImport[][string] getImportedModules() {
        return myImportedModules;
    }

    override public DDeclaration[] getDeclarations() {
        return myDeclarations;
    }

    override public void accept(DVisitor visitor) {
        visitor.visitModule(this);
    }
}
